package org.easyway.objects.sprites2D.sentry;

import java.io.Serializable;
import java.util.ArrayList;

import org.easyway.collisions.GroupCollision;
import org.easyway.collisions.SingleCollisionList;
import org.easyway.interfaces.base.ITexture;
import org.easyway.interfaces.sprites.ISpriteColl;
import org.easyway.lists.BaseList;
import org.easyway.objects.sprites2D.SpriteColl;
import org.easyway.tiles.TileManager;
import org.easyway.tiles.TileSprite;
import org.easyway.tiles.TiledCollisionList;

public class SentrySprite extends SpriteColl implements Serializable {

    private static final long serialVersionUID = -5139860575042410163L;
    public ArrayList<Sentry> sentryList = new ArrayList<Sentry>(5);

    public SentrySprite() {
        super();
    }

    public SentrySprite(boolean quad) {
        super(quad);
    }

    public SentrySprite(float x, float y, boolean quad) {
        super(x, y, quad);
    }

    public SentrySprite(float x, float y, float z, boolean quad) {
        super(x, y, z, quad);
    }

    public SentrySprite(float x, float y, float z, ITexture img, int layer,
            boolean quad) {
        super(x, y, z, img, layer, quad);
    }

    public SentrySprite(float x, float y, float z, ITexture img, int layer) {
        super(x, y, z, img, layer);
    }

    public SentrySprite(float x, float y, float z) {
        super(x, y, z);
    }

    public SentrySprite(float x, float y, int layer, boolean quad) {
        super(x, y, layer, quad);
    }

    public SentrySprite(float x, float y, int layer) {
        super(x, y, layer);
    }

    public SentrySprite(float x, float y, ITexture img, boolean quad) {
        super(x, y, img, quad);
    }

    public SentrySprite(float x, float y, ITexture img, int layer, boolean quad) {
        super(x, y, img, layer, quad);
    }

    public SentrySprite(float x, float y, ITexture img, int layer) {
        super(x, y, img, layer);
    }

    public SentrySprite(float x, float y, ITexture img) {
        super(x, y, img);
    }

    public SentrySprite(float x, float y) {
        super(x, y);
    }

    public SentrySprite(int layer, boolean quad) {
        super(layer, quad);
    }

    public SentrySprite(int layer) {
        super(layer);
    }

    public SentrySprite(ITexture img, boolean quad) {
        super(img, quad);
    }

    public SentrySprite(ITexture img, int layer, boolean quad) {
        super(img, layer, quad);
    }

    public SentrySprite(ITexture img, int layer) {
        super(img, layer);
    }

    public SentrySprite(ITexture img) {
        super(img);
    }

    public SentrySprite(SentrySprite obj) {
        super(obj);
        for (Sentry sentry : obj.sentryList) {
            addSentry((Sentry) sentry.clone());
        }
    }

    // --------------------------------------------------
    /** TO OVERRIDE */
    public void sentryCollision(Sentry sentry) {

    }

    public void addSentry(Sentry sentry) {
        sentryList.add(sentry);
    }

    public Sentry addSentry(float x, float y, int width, int height, String name) {
        Sentry t;
        sentryList.add(t = new Sentry(this, name, x, y, width, height));
        return t;
    }

    public void removeSentry(Sentry sentry) {
        sentryList.remove(sentry);
    }

    public void removeSentry(String name) {
        for (Sentry s : sentryList) {
            if (s.getType().get().equals(name)) {
                s.destroy();
            }
        }
    }

    // ---------------------------------------------------
    protected void updateSentinels() {
        float tx = getX();
        float ty = getY();
        for (Sentry s : sentryList) {
            s.setXY(tx, ty);
        }
    }

    @Override
    public void incX() {
        super.incX();
        for (Sentry s : sentryList) {
            s.setXY(s.getX() + 1, s.getY());
        }
    }

    @Override
    public void incX(float incx) {
        super.incX(incx);
        for (Sentry s : sentryList) {
            s.setXY(s.getX() + incx, s.getY());
        }
    }

    @Override
    public void decX() {
        super.decX();
        for (Sentry s : sentryList) {
            s.setXY(s.getX() - 1, s.getY());
        }

    }

    @Override
    public void decY() {
        super.decY();
        for (Sentry s : sentryList) {
            s.setXY(s.getX(), s.getY() - 1);
        }

    }

    @Override
    public void incY() {
        super.incY();
        for (Sentry s : sentryList) {
            s.setXY(s.getX(), s.getY() + 1);
        }

    }

    @Override
    public void incY(float incy) {
        super.incY(incy);
        for (Sentry s : sentryList) {
            s.setXY(s.getX(), s.getY() + incy);
        }

    }

    @Override
    public void move(float incx, float incy) {
        super.move(incx, incy);
        for (Sentry s : sentryList) {
            s.setXY(s.getX() + incx, s.getY() + incy);
        }

    }

    @Override
    public void setX(float x) {
        float step = x - getXIndipendent();
        super.setX(x);
        for (Sentry s : sentryList) {
            s.setXY(s.getX() + step, s.getY());
        }

    }

    @Override
    public void setXY(float x, float y) {
        float stepx = x - getXIndipendent();
        float stepy = y - getYIndipendent();
        super.setXY(x, y);
        for (Sentry s : sentryList) {
            s.setXY(s.getX() + stepx, s.getY() + stepy);
        }
    }

    @Override
    public void setY(float y) {
        float step = y - getYIndipendent();
        super.setY(y);
        for (Sentry s : sentryList) {
            s.setXY(s.getX(), s.getY() + step);
        }
    }

    public void autoAlign(float stepx, float stepy, Sentry source, TileManager tm, String tileName) {
        ArrayList<ISpriteColl> coll = tm.testCollision(source);
        boolean found = false;
        do {
            coll = tm.testCollision(source);
            TileSprite tSprite;
            found = false;
            for (ISpriteColl spr : coll) {
                tSprite = (TileSprite)spr;
                if (tSprite.getTileString().equals(tileName)) {
                    found = true;
                    break;
                }
            }
            move(stepx, stepy);
        } while (found);

    }

    public Object clone() {
        return new SentrySprite(this);
    }
}
